//
//  BAIndexVC.m
//  YunFu
//
//  Created by Bager on 2018/9/3.
//  Copyright © 2018年 yunfu. All rights reserved.
//

#import "BAIndexVC.h"

#import "CyclePagerView.h"
#import "BAIndexCollectionView.h"
#import "BANearbyBrandView.h"
#import "BABrandOnlineView.h"
#import "BADiscountsView.h"
#import "BAShopCell.h"
#import "BADetailVC.h"
#import "ZLPhotoActionSheet.h"

#import "HistorySearchVC.h"
#import "BAIndexVM.h"


@interface BAIndexVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) BAIndexCollectionView *indexCV;
@property (nonatomic, strong) BANearbyBrandView *nearbyBrandView;
@property (nonatomic, strong) BABrandOnlineView *brandOnlineView;
@property (nonatomic, strong) BADiscountsView *discountsView;
@property (nonatomic, strong) BAIndexVM *viewModel;

@end

@implementation BAIndexVC

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self createHeader];
    
    [self createTable];
    
//    [self getInfo];
}

- (void)createHeader
{
    UIView *headerBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenHeight, kPublicNavHeight)];
    headerBg.backgroundColor = kPublicColorMain;
    [self.view addSubview:headerBg];
    
    [self creatSearchNaviBar];
}

- (void)creatSearchNaviBar
{
    LLSearchNaviBarView *searchNaviBarView = [LLSearchNaviBarView new];
    searchNaviBarView.searbarPlaceHolder = @"请输入搜索关键词";
    
    @LLWeakObj(self);
    [searchNaviBarView showbackBtnWith:[UIImage imageNamed:@"navi_back_w"] onClick:^(UIButton *btn) {
        @LLStrongObj(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [searchNaviBarView showbackBtnWith:[UIImage imageNamed:@"back"] onClick:^(UIButton *btn) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [searchNaviBarView showRightOneBtnWith:[UIImage imageNamed:@"sandian"] onClick:^(UIButton *btn) {
        
    }];
    
    [searchNaviBarView setSearchBarBeignOnClickBlock:^{
        @LLStrongObj(self);
        
        HistorySearchVC *searShopVC = [HistorySearchVC new];
        
        
        @LLWeakObj(searShopVC);
        //(1)点击分类 (2)用户点击键盘"搜索"按钮  (3)点击历史搜索记录
        [searShopVC beginSearch:^(NaviBarSearchType searchType,NBSSearchShopCategoryViewCellP *categorytagP,UILabel *historyTagLabel,LLSearchBar *searchBar) {
            @LLStrongObj(searShopVC);
            
            NSLog(@"搜索结果：historyTagLabel:%@--->searchBar:%@--->categotyTitle:%@--->%@",historyTagLabel.text,searchBar.text,categorytagP.categotyTitle,categorytagP.categotyID);
            
            searShopVC.searchBarText = @"你选择的搜索内容显示到这里";
        }];
        
        //执行即时搜索匹配
        NSArray *tempArray = @[@"Java", @"Python"];
        
        [searShopVC searchbarDidChange:^(NaviBarSearchType searchType, LLSearchBar *searchBar, NSString *searchText) {
            @LLStrongObj(searShopVC);
            
            NSLog(@"搜索文字++++++%@", searchText);
            
            //FIXME:这里模拟网络请求数据!!!
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                searShopVC.resultListArray = tempArray;
            });
        }];
        
        [searShopVC resultListViewDidSelectedIndex:^(UITableView *tableView, NSInteger index) {
            NSLog(@"搜索结果++++++%@", tempArray[index]);
        }];
        
        [self presentViewController:searShopVC animated:nil completion:nil];
    }];
    
    [self.view addSubview:searchNaviBarView];
}


- (void)createTable
{
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, kPublicNavHeight, kScreenWidth, kScreenHeight - kPublicNavHeight)];
    table.delegate = self;
    table.dataSource = self;
    table.showsVerticalScrollIndicator = NO;
    table.backgroundColor = HexColor(0xf4f5f6);
//    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    
    UIView *noLine = [[UIView alloc] initWithFrame:CGRectZero];
    table.tableFooterView = noLine;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 180 + kPublicPadding;
    }
    else if (indexPath.row == 1)
    {
        return ((kScreenWidth - kPublicMargin * 2) / 5.0 + 20) * 2 + 10;
    }
    else if (indexPath.row == 2)
    {
        return 150 + 44 + 10;
    }
    else if (indexPath.row == 3)
    {
        return 150 + 44 + 10;
    }
    else if (indexPath.row == 4)
    {
        return 140 + 44;
    }
    else if (indexPath.row == 5)
    {
        return 44;
    }
    
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < 6)
    {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = HexColor(0xf4f5f6);
        
        if (indexPath.row == 0)
        {
            CyclePagerView *page = [[CyclePagerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 180) imgArray:@[@"",@"",@""]];
            [cell addSubview:page];
            
            DefineWeakSelf;
            page.callBack = ^(id result) {
                
            };
        }
        else if (indexPath.row == 1)
        {
            [cell addSubview:self.indexCV];
        }
        else if (indexPath.row == 2)
        {
            [cell addSubview:self.nearbyBrandView];
        }
        else if (indexPath.row == 3)
        {
            [cell addSubview:self.brandOnlineView];
        }
        else if (indexPath.row == 4)
        {
            [cell addSubview:self.discountsView];
        }
        else if (indexPath.row == 5)
        {
            UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(80, 21, kScreenWidth - 80 * 2, 1)];
            line.backgroundColor = HexColor(0x0c97ec);
            [cell addSubview:line];
            
            UILabel *lineText = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 2.0 - 45, 0, 90, 44)];
            lineText.text = @"为你定制";
            lineText.font = [UIFont systemFontOfSize:14];
            lineText.textAlignment = NSTextAlignmentCenter;
            lineText.backgroundColor = HexColor(0xf4f5f6);
            lineText.textColor = HexColor(0x0c97ec);
            [cell addSubview:lineText];
        }
        cell.separatorInset = UIEdgeInsetsMake(0, kScreenWidth, 0, 0);
        return cell;
    }
    else
    {
        BAShopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifiers"];
        if (!cell)
        {
            cell = [[BAShopCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellIdentifiers"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row > 5)
    {
        BADetailVC *detail = [BADetailVC new];
        [self.navigationController pushViewController:detail animated:YES];
    }
}

- (BAIndexCollectionView *)indexCV
{
    if (!_indexCV)
    {
        _indexCV = [[BAIndexCollectionView alloc] init];
    }
    return _indexCV;
}

- (BANearbyBrandView *)nearbyBrandView
{
    if (!_nearbyBrandView)
    {
        _nearbyBrandView = [[BANearbyBrandView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150 + 44)];
    }
    return _nearbyBrandView;
}

- (BABrandOnlineView *)brandOnlineView
{
    if (!_brandOnlineView)
    {
        _brandOnlineView = [[BABrandOnlineView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150 + 44)];
    }
    return _brandOnlineView;
}

- (BADiscountsView *)discountsView
{
    if (!_discountsView)
    {
        _discountsView = [[BADiscountsView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 140 + 44)];
    }
    return _discountsView;
}

- (BAIndexVM *)viewModel
{
    if (!_viewModel)
    {
        _viewModel = [BAIndexVM new];
    }
    return _viewModel;
}

- (void)getInfo
{
    [self.viewModel getInfo:@{@"category":@"app"} viewForLoading:self.view success:^(id responseObject) {
        
    }];
}

@end
